﻿package Util 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author Gregory Weir
	 */
	public class SpriteSheet 
	{
		private var bmSheet:BitmapData;
		
		private var rows:int;
		private var cols:int;
		
		private var frameWidth:int;
		private var frameHeight:int;
		
		public function SpriteSheet(bm:Bitmap, r:int, c:int ) 
		{
			bmSheet = bm.bitmapData;
			rows = r;
			cols = c;
			
			frameWidth = bmSheet.width / cols;
			frameHeight = bmSheet.height / rows;
		}
		
		public function getFrame(r:int, c:int):BitmapData
		{
			var f:BitmapData = new BitmapData(frameWidth, frameHeight);
			
			f.copyPixels(bmSheet, new Rectangle(c * f.width, r * f.height, f.width, f.height), new Point(0, 0));
			
			return f;
		}
		
		public function copyFrame(r:int, c:int, target:BitmapData, targetx:int, targety:int):void
		{
			target.copyPixels(bmSheet, new Rectangle(c * frameWidth, r * frameHeight, frameWidth, frameHeight), new Point(targetx, targety));
		}
	}
	
}